package com.wiyun.engine.skeleton;

import java.io.IOException;

import android.content.res.AssetManager;
import android.view.MotionEvent;

import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.INodeVirtualMethods;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.nodes.Animation.IAnimationCallback;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.tmx.TMXTileMap;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYSize;

public class PKLayer extends Layer implements IAnimationCallback, INodeVirtualMethods  {
	TMXTileMap mMap;
	public PKLayer(AssetManager am) throws IOException
	{
		m_asset=am;
		CreateMap();
		try {
			CreatePlayer();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void CreateMap()
	{
		Texture2D tex11 = Texture2D.makeJPG(R.drawable.bg2_00x00);
		Texture2D tex12 = Texture2D.makeJPG(R.drawable.bg2_00x01);
		
		Texture2D tex21 = Texture2D.makeJPG(R.drawable.bg2_01x00);
		Texture2D tex22 = Texture2D.makeJPG(R.drawable.bg2_01x01);
		tex11.autoRelease();
		tex12.autoRelease();

		tex21.autoRelease();
		tex22.autoRelease();
		mMap = TMXTileMap.make(R.raw.pk_layer,tex11, tex12,
		tex21, tex22);
		mMap.setDebugDrawObjects(true);
        addChild(mMap);
        
        setTouchEnabled(true);
	}

	String role="player_nanzhanshi";
	Sprite mPlayer;
	AssetManager m_asset;
	AnimationRole _playerAnimation;
	float _speed=100;
	public void CreatePlayer() throws IOException
	{
		WYSize s = Director.getInstance().getWindowSize();
		mPlayer = Sprite.make(Texture2D.makePNG(R.drawable.a_up1));
     	mPlayer. setAnchorY(0);
		mPlayer.setPosition(400, 400);
		addChild(mPlayer);
     	mPlayer.scale(0.75f);
		//player_left_anim = LoadAnimation(role+File.separator+"run_left", 0, animDurp);
		_playerAnimation = new AnimationRole(role, 0.15f, _speed, this, m_asset); 

     	WYPoint loc = Director.getInstance().convertToGL(100, 100);
     	_playerAnimation.RunAnimation(mPlayer, loc.x, loc.y);
		//IntervalAction anim = (IntervalAction)Animate.make(player_left_anim, true).autoRelease();
	}
	@Override
    public boolean wyTouchesBegan(MotionEvent event) {
		/*Scene scene = Scene.make();
        try {
			scene.addChild(new MainLayer(m_asset), 0);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
        Director.getInstance().popScene();
     	
     	return true;
 }
	@Override
	public void jDraw() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void jOnEnter() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void jOnEnterTransitionDidFinish() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void jOnExit() {
		// TODO Auto-generated method stub
		_playerAnimation.Dispose();
	}
	@Override
	public void onAnimationEnded(int arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onAnimationFrameChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
}
